package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.PlayerMapper;
import com.kkb.pojo.Player;
import com.kkb.pojo.PlayerExample;
import com.kkb.pojo.Team;
import com.kkb.vo.PlayerQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 平川
 * @Description:
 * @Date Created in 2021-10-03 11:49
 * @Modified By:
 */
@Service
public class PlayerService {
    @Resource
    private PlayerMapper playerMapper;  //加Resource就不报错，加AutoWire就报错
    @Resource
    private TeamService teamService;
    /**
     * 根据多条件查询球员信息
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<Player> queryByPage(Integer pageNum, Integer pageSize, PlayerQueryVo vo){
        PlayerExample example = new PlayerExample();
        PlayerExample.Criteria criteria = example.createCriteria();
        if (vo!=null){
            if (vo.getPlayerName()!=null && !"".equals(vo.getPlayerName().trim())){
                criteria.andPlayerNameLike("%"+vo.getPlayerName()+"%");
            }
            if (vo.getPlayerNum()!=null && vo.getPlayerNum()>0){
                criteria.andPlayerNumEqualTo(vo.getPlayerNum());
            }
            if (vo.getLocation()!=null && !"".equals(vo.getLocation().trim())){
                criteria.andLocationLike("%"+vo.getLocation()+"%");
            }
            if (vo.getBeginDate()!=null){
                criteria.andBirthdayGreaterThanOrEqualTo(vo.getBeginDate());
            }
            if (vo.getEndDate()!=null){
                criteria.andBirthdayLessThanOrEqualTo(vo.getEndDate());
            }
            if (vo.getStatus()!=null && vo.getStatus()!=-1){
                criteria.andStatusEqualTo(vo.getStatus());
            }
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Player> players = playerMapper.selectByExample(example);
        for (Player player : players) {
            Team team = teamService.queryById(player.getTeamId());
            player.setTeam(team);
        }
        return new PageInfo<Player>(players);
    }

    /**
     * 添加一个球员
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int addPlayer(Player player){
        return playerMapper.insertSelective(player);
    }
    /**
     * 根据id查询球员信息
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Player queryById(Integer playerId){
        Player player = playerMapper.selectByPrimaryKey(playerId);

        return player;
    }
    /**
     * 更新球员信息
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updatePlayer(Player player){
        return playerMapper.updateByPrimaryKeySelective(player);
    }
    /**
     * 删除一条球员信息
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int deletePlayer(Integer playerId){
        return playerMapper.deleteByPrimaryKey(playerId);
    }
}
